<?php

if ( ! function_exists('sub_str')) {
    /**
     * PHP截取文字长度
     * @return string
     */
    function sub_str($str, $len = 2)
    {
        $strlen = strlen($str) / 3;#在编码utf8下计算字符串的长度，并把它交给变量$strlen
        #echo $strlen;#输出字符串长度
        if ($strlen < $len) {
            return $str;
        } else {
            return mb_substr($str, 0, $len, "utf-8")."...";
        }
    }
}

if ( ! function_exists('get_tree')) {
//递归返回树形菜单数据
    function get_tree($data, $pId, $open = 0, $deep = 0)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            $v['checkArr'] = ['type' => 0, 'isChecked' => 0];
            $v['spread']   = true;
            $v['parentId'] = $v['pid'];
            if ($deep >= $open) {
                $v['spread'] = true;
            }
            $v['name'] = $v['title'];
            if ($v['pid'] == $pId) {
                //父亲找到儿子
                $deep++;
                $v['children'] = get_tree($data, $v['id'], $open, $deep);
                $tree[]        = $v;
            }
        }

        return array_values($tree);
    }
}
